<?php

// CSS Functions

function ink_elements($element_string) {
		global $wpdb, $table_prefix;
		$table = $table_prefix.'ink';
		$element_string = chop(preg_replace('/,\s+/', '^^^', 'body, a:link, a:hover, a:visited, '.$element_string));
		$elements = explode('^^^', $element_string);
		$theme = get_option('template');
	
		foreach($elements as $element) {
			if(!$wpdb->get_var("SELECT element FROM ".$table." WHERE element = '$element' AND theme = '$theme'"))
				$wpdb->query("INSERT INTO ".$table." (element, theme) VALUES ('$element','$theme')");
		}
		if(!get_option('ink_css')) write_ink_definitions();
}

function ink_create_css($element, $index, $definition) {
	$css_element = $element.' {'."\n";
		if($index == 'color') $css_element .= "\t".'color: #'.$definition.';'."\n";
		if($index == 'background') $css_element .= "\t".'background-color: #'.$definition.';'."\n";
		if($index == 'border') $css_element .= "\t".'border-color: #'.$definition.';'."\n";
		if($index == 'font_family' && $definition != 'none') $css_element .= "\t".'font-family: '.$definition.';'."\n";
		if($index == 'font_size') $css_element .= "\t".'font-size: '.$definition.'px;'."\n";
		if($index == 'font_style' && strstr($definition, 'normal') && $definition != 'none') $css_element .= "\t".'font-style: normal;'."\n"."\t".'font-weight: normal;'."\n";
		if($index == 'font_style' && strstr($definition, 'italic') && $definition != 'none') $css_element .= "\t".'font-style: italic;'."\n";
		if($index == 'font_style' && strstr($definition, 'bold') && $definition != 'none') $css_element .= "\t".'font-weight: bold;'."\n";
		if($index == 'other') $css_element .= "\t".$definition."\n";
	$css_element .= '}'."\n\n";
	
	return $css_element;
}

function write_ink_definitions() {
	global $wpdb, $table_prefix;
	$table = $table_prefix.'ink';
	$theme = get_option('template');
	$css = '/* CSS Generated by Ink - http://freshpursuits.com/ink */'."\n\n";
	if($values = $wpdb->get_results("SELECT * FROM ".$table." WHERE theme = '$theme'")) {
		foreach($values as $value) {
			$definitions = php4_clone($value);
			unset($definitions->element, $definitions->element_id, $definitions->theme);
			foreach($definitions as $index => $definition) {
				if(!empty($definition)) $css .= ink_create_css($value->element, $index, $definition);
			}
		}
		update_option('ink_css', '<style type="text/css" media="screen">'."\n".$css.'</style>');
	}
}

function php4_clone($object) {
  if (version_compare(phpversion(), '5.0') < 0) {
   return $object;
  } else {
   return @clone($object);
  }
 }

function output_ink() {
	if(get_option('enableInk') == 'true') echo get_option('ink_css');
	}

// Admin Functions

function get_ink_definition($element, $definition) {
	global $wpdb, $table_prefix;
	$table = $table_prefix.'ink';
	$theme = get_option('template');
	$value = $wpdb->get_var("SELECT ".$definition." FROM ".$table." WHERE element = '$element' AND theme = '$theme'");
	return $value;
}

function set_ink_definition($element, $definition, $value) {
	global $wpdb, $table_prefix;
	$table = $table_prefix.'ink';
	$theme = get_option('template');
	$update = $wpdb->query("UPDATE ".$table." SET ".$definition."='$value' WHERE element = '$element' AND theme = '$theme'");
	write_ink_definitions();
	return $update;
}

function ink_clean_install() {
    global $table_prefix, $wpdb;
	$table = $table_prefix . 'ink';

    require_once(ABSPATH . 'wp-admin/upgrade-functions.php');

	$wpdb->query("DROP TABLE IF EXISTS $table");

	$sql = "CREATE TABLE ".$table." (
	        element_id INT NOT NULL AUTO_INCREMENT,
	        PRIMARY KEY(element_id),
   			element text NOT NULL,
   			theme text NOT NULL,
   			color text NOT NULL,
   			background text NOT NULL,
   			border text NOT NULL,
   			font_family text NOT NULL,
   			font_size text NOT NULL,
   			font_style text NOT NULL,
   			other text NOT NULL
			);";
    dbDelta($sql);
	update_option('enableInk', 'false');
	
	canvas_check_theme();
}

function ink_highlight() {
	if(isset($_GET["ink_highlight"])) {
		echo '<style type="text/css" media="screen">'."\n".urldecode($_GET["ink_highlight"]).' { border: 1px solid red; } </style>';
	}
}

require_once('ink-admin.php');
?>
